<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: FieldsController.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_FieldsController extends Core_Controller_Action_Standard
{
	public function editAction() 
	{
		$page = $this->_getParam('page');
		
		$this->view->pageObject = $pageObject = Engine_Api::_()->getItem('page', $page);
		$aliasedFields = $pageObject->fields()->getFieldsObjectsByAlias();
		$this->view->topLevelId = $topLevelId = 0;
    $this->view->topLevelValue = $topLevelValue = null;

		if( isset($aliasedFields['profile_type']) ) {
      $aliasedFieldValue = $aliasedFields['profile_type']->getValue($pageObject);
      $topLevelId = $aliasedFields['profile_type']->field_id;
      $topLevelValue = ( is_object($aliasedFieldValue) ? $aliasedFieldValue->value : null );
      if( !$topLevelId || !$topLevelValue ) {
        $topLevelId = null;
        $topLevelValue = null;
      }
      $this->view->topLevelId = $topLevelId;
      $this->view->topLevelValue = $topLevelValue;
    }
    
    $form = $this->view->form = new Fields_Form_Standard(array(
      'item' => $pageObject,
      'topLevelId' => $topLevelId,
      'topLevelValue' => $topLevelValue,
    ));
    
		// Not posting
    if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ){
      $form->saveValues();

      // Update display name
      $aliasValues = Engine_Api::_()->fields()->getFieldsValuesByAlias($pageObject);
      
      // $form->addNotice(Zend_Registry::get('Zend_Translate')->_('Your changes have been saved.'));
      return $this->_redirect("page/editor/index?page={$page}");
    }
	}
}